package title4

import java.util.Properties

import org.apache.spark.sql.{Dataset, SaveMode, SparkSession}

object DealWithRule {
  def main(args: Array[String]): Unit = {

    val session = SparkSession.builder().master("local[*]").appName(this.getClass.getName).getOrCreate()
    import session.implicits._
    //获取rule数据 并分割
    val ruleSourceData = session.read.textFile("data/rule.txt")
    val data = ruleSourceData.map({ t =>
      val line = t.split(" ")
      val id = line(0)
      val order = line(1)
      val brand = line(2)
      (id, order, brand)
    }).toDF("id", "order", "brand")

    //将rule数据存储到mysql
    val url = "jdbc:mysql://localhost:3306/scott?characterEncoding=utf-8"
    val tname = "rule"
    val p = new Properties()
    p.setProperty("user", "root")
    p.setProperty("password", "123456")
    p.setProperty("driver", "com.mysql.jdbc.Driver")
    data.write.mode(SaveMode.Overwrite).jdbc(url, tname, p)

  }
}
